package com.ruitu.health_1.ui.fragment.order;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.google.gson.Gson;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.GoodsInfo;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.dao.bean.Pay;
import com.ruitu.health_1.dao.bean.StoreOrderItemList;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.OrderPresenter;
import com.ruitu.health_1.ui.CommentActivity;
import com.ruitu.health_1.ui.CommentListActivity;
import com.ruitu.health_1.ui.OrderDetailsActivity;
import com.ruitu.health_1.ui.PayOrderActivity;
import com.ruitu.health_1.ui.ShippedActivity;
import com.ruitu.health_1.util.CustomDialogUtil;
import com.ruitu.health_1.util.CustomInterface;
import com.ruitu.health_1.view.NoCacheLoadingFragment;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.Logg;
import com.ruitu.router_module.bean.user.Address;
import com.ruitu.router_module.util.ReqUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AllOrderFragment extends NoCacheLoadingFragment<OrderPresenter.OrderP, OrderModel.OrderM> {
    public static AllOrderFragment newInstance() {

        Bundle args = new Bundle();

        AllOrderFragment fragment = new AllOrderFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private List<Integer> skipId;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_order_list;
    }

    @Override
    protected void initViews(View v) {
        skipId = new ArrayList<>();
        showLoadingView(skipId);
        getDataList();
    }

    @Override
    protected ProgressLayout findProgressLayout(View v) {
        return v.findViewById(R.id.progress);
    }

    @Override
    protected LRecyclerView findRecyclerView(View v) {
        return v.findViewById(R.id.l_recyclerview);
    }

    private StoreDataAdapter adapter;

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new StoreDataAdapter(getContext());
        return adapter;
    }

    @Override
    protected void showLoadingView(List<Integer> skipIds) {
        super.showLoadingView(skipIds);
    }

    @Override
    protected void reqFirst() {
        size = 10;
        getDataList();
    }

    private int size = 10;

    @Override
    protected void reqMore() {
        size = size + 10;
        getDataList();
    }

    private void getDataList() {
        p.orderList(1, size, AppHolder.instance().getUser().getId(), "");
    }

    @Override
    public void setData(List<StoreOrderItemList> orderLists) {
        showContent(skipId);
        adapter.setDataList(orderLists);
    }

    private LinearLayout ll_wait_pay;
    private LinearLayout ll_wait_send;
    private LinearLayout ll_shipped;
    private LinearLayout ll_complete;
    private TextView tv_go_pay;//去支付
    private TextView tv_cancel;//取消
    private TextView tv_send_cancel;//取消
    private TextView tv_pay_detail;//待支付详情
    private TextView tv_send_detail;//待发货详情
    private TextView tv_check_logistics;//查看物流
    private TextView tv_confirm_receipt;//确认收货
    private TextView tv_shipped_detail;//待收货详情
    private TextView tv_comment;//评论
    private TextView tv_buy_again;//再次购买
    private TextView tv_complete_detail;//完成订单的详情
    private TextView tv_order_status;//订单的状态

//    class DataAdapter extends ListBaseAdapter<OrderList> {
//
//        public DataAdapter(Context context) {
//            super(context);
//        }
//
//        @Override
//        public int getLayoutId() {
//            return R.layout.item_order_list;
//        }
//
//        @Override
//        public void onBindItemHolder(SuperViewHolder holder, int position) {
//            final OrderList orderList = mDataList.get(position);
//            RecyclerView recyclerview = holder.getView(R.id.recyclerview);
//            View view = holder.getView(R.id.divider);
//            tv_go_pay = holder.getView(R.id.tv_go_pay);
//            tv_cancel = holder.getView(R.id.tv_cancel);
//            tv_pay_detail = holder.getView(R.id.tv_pay_detail);
//            ll_wait_pay = holder.getView(R.id.ll_wait_pay);
//
//            view.setVisibility(position > 0 ? View.VISIBLE : View.GONE);
//            String status = orderList.getOrder().getStatus();
//            ll_wait_pay.setVisibility(TextUtils.equals(status, "0") ? View.VISIBLE : View.GONE);
//            LinearLayoutManager manager = new LinearLayoutManager(mContext);
//            recyclerview.setLayoutManager(manager);
//            final ArrayList<StoreOrderItemList> storeOrderItemList = orderList.getStoreOrderItemList();
//            for (int i = 0; i < storeOrderItemList.size(); i++) {
//                storeOrderItemList.get(i).setOrderStatus(orderList.getOrder().getStatus());
//            }
//            StoreDataAdapter storeDataAdapter = new StoreDataAdapter(mContext);
//            recyclerview.setAdapter(storeDataAdapter);
//            storeDataAdapter.setDataList(orderList.getStoreOrderItemList());
//            //去支付
//            tv_go_pay.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Pay pay = new Pay();
//                    pay.setOrderId(orderList.getOrder().getId());
//                    pay.setPayAmount(orderList.getOrder().getPayAmount());
//                    Bundle bundle = new Bundle();
//                    bundle.putSerializable("address", orderList.getOrder().getReceiveAddress());
//                    bundle.putSerializable("pay", pay);
//                    startActivity(PayOrderActivity.class, bundle);
//                }
//            });
//            //取消订单
//            tv_cancel.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    dialogUtil = new CustomDialogUtil(getContext(), "是否取消当前订单", new CustomInterface.OnBtnClickListener() {
//                        @Override
//                        public void onLeftClick() {
//                            Map<String, String> map = new HashMap<>();
//                            map.put("orderId", orderList.getOrder().getId());
//                            map.put("memberId", AppHolder.instance().getUser().getId());
//                            p.cancelOrder(ReqUtil.getBody(map, true));
//                        }
//
//                        @Override
//                        public void onRightClick() {
//                            dialogUtil.dismiss();
//                        }
//                    });
//                    dialogUtil.show();
//                }
//            });
//
//            //待支付订单详情
//            tv_pay_detail.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
////                    setOnOrderDetail(storeOrderItemList);
//                }
//            });
//        }
//    }

    @Override
    public void setWaitPayData(Pay payData) {
        super.setWaitPayData(payData);
        Bundle bundle = new Bundle();
        Address address = new Address();
        address.setName(payData.getReceiverName());
        address.setPhoneNumber(payData.getReceiverPhone());
        address.setProvince(payData.getReceiverProvince());
        address.setCity(payData.getReceiverCity());
        address.setRegion(payData.getReceiverRegion());
        address.setDetailAddress(payData.getReceiverDetailAddress());
        bundle.putSerializable("address", address);
        bundle.putSerializable("pay", payData);
        startActivity(PayOrderActivity.class, bundle);
    }

    @Override
    public void requestSuc() {
        super.requestSuc();
        if (dialogUtil != null) {
            dialogUtil.dismiss();
        }
        getDataList();
    }

    private CustomDialogUtil dialogUtil;
    private List list;
    //订单中操作按钮
    private void setOnClick(final StoreOrderItemList orderItemList) {
        final ArrayList<OrderItemList> orderItemList1 = orderItemList.getOrderItemList();
        tv_go_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = new ArrayList();
                for (int i = 0; i < orderItemList1.size(); i++) {
                    list.add(orderItemList1.get(i).getId());
                }
                Map map = new HashMap();
//                map.put("shopId", orderItemList.getStoreId());
                map.put("ids", new Gson().toJson(list));
                showProgress();
                p.payOldorder(ReqUtil.getBody(map, true));
            }
        });

        tv_pay_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnOrderDetail(orderItemList);
            }
        });

        //发货详情
        tv_send_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnOrderDetail(orderItemList);
            }
        });
        //查看物流
        tv_check_logistics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("暂无物流信息");
            }
        });
        tv_confirm_receipt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String orderIds = new Gson().toJson(ids(orderItemList));
                final Map map = new HashMap<>();
                map.put("ids", orderIds);
                map.put("memberId", AppHolder.instance().getUser().getId());

                if (orderItemList1.size() <= 1) {
                    dialogUtil = new CustomDialogUtil(getContext(), "您是否收到该订单商品", "已收货", "未收货", new CustomInterface.OnBtnClickListener() {
                        @Override
                        public void onLeftClick() {
                            showProgress();
                            p.confirmReceipt(ReqUtil.getBody(map, true));
                        }

                        @Override
                        public void onRightClick() {
                            dialogUtil.dismiss();
                        }
                    });
                    dialogUtil.show();
                    return;
                }
                //选择确认收货
                dialogUtil = new CustomDialogUtil(getContext(), "请选择确认收货方式", "全部确认", "单选确认"
                        , new CustomInterface.OnBtnClickListener() {
                    @Override
                    public void onLeftClick() {

                        showProgress();
                        p.confirmReceipt(ReqUtil.getBody(map, true));
                    }

                    @Override
                    public void onRightClick() {
                        Bundle bundle = new Bundle();
                        bundle.putParcelableArrayList("order", orderItemList1);
                        startActivity(ShippedActivity.class);
                    }
                });
                dialogUtil.show();
            }
        });
        //待收货详情
        tv_shipped_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnOrderDetail(orderItemList);
            }
        });
        //评论
        tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (orderItemList.getOrderItemList().size() <= 1) {
                    Bundle bundle = new Bundle();
                    OrderItemList orderItemList1 = orderItemList.getOrderItemList().get(0);
                    bundle.putString("orderId", orderItemList1.getId());
                    bundle.putString("pic", orderItemList1.getProductPic());
                    startActivity(CommentActivity.class, bundle);
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable("order", orderItemList.getOrderItemList());
                startActivity(CommentListActivity.class, bundle);
            }
        });
        //再次购买
        tv_buy_again.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        //完成的详情
        tv_complete_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnOrderDetail(orderItemList);
            }
        });
        //取消订单
        tv_send_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
    }

    private ArrayList<String> idList;

    //获取所有确认收货订单的id
    private List<String> ids(StoreOrderItemList storeOrderItemList) {
        idList = new ArrayList<>();
        List<OrderItemList> orderItemList = storeOrderItemList.getOrderItemList();
        for (int i = 0; i < orderItemList.size(); i++) {
            idList.add(orderItemList.get(i).getId());
        }
        return idList;
    }


    private void setOnOrderDetail(StoreOrderItemList orderList) {
        idList = new ArrayList<>();
        ArrayList<OrderItemList> orderItemList = orderList.getOrderItemList();
        for (int i = 0; i < orderItemList.size(); i++) {
            idList.add(orderItemList.get(i).getId());
        }
        Bundle bundle = new Bundle();
        bundle.putString("orderId", new Gson().toJson(idList));
        startActivity(OrderDetailsActivity.class, bundle);
    }

    class StoreDataAdapter extends ListBaseAdapter<StoreOrderItemList> {
        public StoreDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_store_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RecyclerView recyclerview = holder.getView(R.id.l_recyclerview);
            TextView tv_store_name = holder.getView(R.id.tv_store_name);
            ll_wait_pay = holder.getView(R.id.ll_wait_pay);
            ll_wait_send = holder.getView(R.id.ll_wait_send);
            ll_shipped = holder.getView(R.id.ll_shipped);
            ll_complete = holder.getView(R.id.ll_complete);
            tv_pay_detail = holder.getView(R.id.tv_pay_detail);
            tv_go_pay = holder.getView(R.id.tv_go_pay);
            tv_cancel = holder.getView(R.id.tv_cancel);
            tv_send_cancel = holder.getView(R.id.tv_send_cancel);
            tv_send_detail = holder.getView(R.id.tv_send_detail);
            tv_check_logistics = holder.getView(R.id.tv_check_logistics);
            tv_confirm_receipt = holder.getView(R.id.tv_confirm_receipt);
            tv_shipped_detail = holder.getView(R.id.tv_shipped_detail);
            tv_comment = holder.getView(R.id.tv_comment);
            tv_buy_again = holder.getView(R.id.tv_buy_again);
            tv_complete_detail = holder.getView(R.id.tv_complete_detail);
            tv_order_status = holder.getView(R.id.tv_order_status);
            StoreOrderItemList storeOrderItemList = mDataList.get(position);

            tv_store_name.setText(storeOrderItemList.getName());
            ChildDataAdapter childDataAdapter = new ChildDataAdapter(mContext);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            recyclerview.setLayoutManager(manager);
            recyclerview.setAdapter(childDataAdapter);
            childDataAdapter.setDataList(storeOrderItemList.getOrderItemList());
            initOrderStatus(storeOrderItemList.getOrder().getStatus());
            setOnClick(storeOrderItemList);
        }
    }

    class ChildDataAdapter extends ListBaseAdapter<OrderItemList> {

        public ChildDataAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_child_order;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);

            OrderItemList listBean = mDataList.get(position);
            Glide.with(mContext).load(listBean.getProductPic()).apply(Arad.opts).into(iv_commodity);

            tv_commodity_name.setText(listBean.getProductName());
//            tv_commodity_num.setText(listBean.getRealAmount());
            tv_commodity_price.setText("￥"+listBean.getRealAmount());
            StringBuffer buffer = new StringBuffer();
            GoodsInfo good = listBean.getGood();
            if (good != null) {
                if (!TextUtils.isEmpty(good.getSp1())) {
                    buffer.append(good.getSp1());
                }
                if (!TextUtils.isEmpty(good.getSp2())) {
                    buffer.append("," + good.getSp2());
                }
                if (!TextUtils.isEmpty(good.getSp3())) {
                    buffer.append("," + good.getSp3());
                }
                Logg.i("Sku:  " + buffer.toString());
                tv_commodity_info.setText(buffer.toString().trim());
            }


        }
    }

    //初始化订单状态
    private void initOrderStatus(String status) {
        if (TextUtils.equals(status, "0")) {
            ll_wait_pay.setVisibility(View.VISIBLE);
            ll_wait_send.setVisibility(View.GONE);
            ll_shipped.setVisibility(View.GONE);
            ll_complete.setVisibility(View.GONE);
            tv_order_status.setText("待付款");
        } else if (TextUtils.equals(status, "1")) {
            ll_wait_pay.setVisibility(View.GONE);
            ll_wait_send.setVisibility(View.VISIBLE);
            ll_shipped.setVisibility(View.GONE);
            ll_complete.setVisibility(View.GONE);
            tv_order_status.setText("待发货");
        } else if (TextUtils.equals(status, "2")) {
            ll_wait_pay.setVisibility(View.GONE);
            ll_wait_send.setVisibility(View.GONE);
            ll_shipped.setVisibility(View.VISIBLE);
            ll_complete.setVisibility(View.GONE);
            tv_order_status.setText("已发货");
        } else if (TextUtils.equals(status, "3")) {
            ll_wait_pay.setVisibility(View.GONE);
            ll_wait_send.setVisibility(View.GONE);
            ll_shipped.setVisibility(View.GONE);
            ll_complete.setVisibility(View.VISIBLE);
            tv_order_status.setText("已完成");
        }
    }

}
